package com.financing.platform.task;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.dao.IActivitesDao;
import com.financing.platform.dao.IFpFaUserDao;
import com.financing.platform.dao.IWxUserInfoDao;
import com.financing.platform.dao.IWxUserRewardLogDao;
import com.financing.platform.dto.ActivitesDTO;
import com.financing.platform.dto.FpFaUserDTO;
import com.financing.platform.dto.WxUserInfoDTO;
import com.financing.platform.dto.WxUserRewardLogDTO;
import com.financing.platform.util.DateUtil;
import com.financing.platform.util.StringUtil;

/**
 * 
 * @Description: 金策师英雄帖添加账单流水
 *
 */
@Component(value = "JcsYxtRewardLogAdd")
public class JcsYxtRewardLogAdd {

	Logger log = Logger.getLogger(this.getClass());


	private String ACTIVITY_CODE = "JCSYXT";

	@Autowired
	private IWxUserRewardLogDao wxUserRewardLogDaoImpl;
	@Autowired
	private IActivitesDao activityDaoImpl;
	@Autowired
	private IFpFaUserDao fpUserDaoImpl;
	@Autowired
	private IWxUserInfoDao wxuserDaoImpl;

//	@Scheduled(cron = "0/30 * * * * ?")
	public void rewardLogAdd() {
		try {
			// 活动结束判断----------------------
			boolean isActivity = true;
			
			ActivitesDTO activity = activityDaoImpl.searchActivitesByCode(ACTIVITY_CODE);
			if(activity==null||StringUtils.isBlank(activity.getActiviesCode())){
				return ;
			}
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// 判断截止时间是否超过
			String startTime = fmt.format(activity.getActiviesStartTime());
			String endTime = fmt.format(activity.getActiviesEndTime());
			
			// 比较大小
			long tms = DateUtil.dateDiffToMinute(fmt.format(new Date()), endTime);
			isActivity = (tms > 0);
			// 判断新认证数是否达到五万
			FpFaUserDTO user = new FpFaUserDTO();
			user.setMonthStart(startTime);
			user.setMonthEnd(endTime);
			user.setActivitiesSource(ACTIVITY_CODE);
			List<FpFaUserDTO> users = fpUserDaoImpl.queryListByAuthDate(user);
			
			
			int alls = fpUserDaoImpl.queryAllByAuthDate(user);
			if (alls >= activity.getActiviesCount()) {
				isActivity = false;
			}
			// 查询认证时间区间
			if (isActivity && users != null) {
				for (int i = 0; i < users.size(); i++) {
					// 添加账单流水
					// 查询微信用户表
					WxUserInfoDTO wxuser1 = new WxUserInfoDTO();
					wxuser1.setUserPhone(users.get(i).getULoginName());
					wxuser1.setUserActivityCode(ACTIVITY_CODE);
					WxUserInfoDTO wxuser = wxuserDaoImpl.loadByUserPhone(wxuser1);
					if (wxuser!=null) {
						// 查询
						WxUserRewardLogDTO wxUserRewardLog = new WxUserRewardLogDTO();
						wxUserRewardLog.setRewardUserId(users.get(i).getId());
						wxUserRewardLog.setActivityCode(ACTIVITY_CODE);
						wxUserRewardLog.setRewardMoney(new BigDecimal(2)); // 查询
						wxUserRewardLog.setDeleteTag("1");
						wxUserRewardLog.setUserPhone(wxuser.getUserPhone());
						// 查询是否需要添加
						List<WxUserRewardLogDTO> ls = wxUserRewardLogDaoImpl.queryList(wxUserRewardLog);
						if (ls.size() == 0) {
							wxUserRewardLog.setRewardDesc("英雄帖活动认证奖励");
							wxUserRewardLog.setCreateBy(users.get(i).getId()+"");
							wxUserRewardLog.setCreateDate(new Date());
							wxUserRewardLog.setRewardStatus(2);
							wxUserRewardLog.setIsRole(2);
							wxUserRewardLog.setRewardSourceUserId(users.get(i).getId());
							wxUserRewardLogDaoImpl.insert(wxUserRewardLog);
						}
						//一级添加
						if (wxuser.getUserParentId()!=null && 
								wxuser.getUserParentId() != 0) {
							FpFaUserDTO d1 = fpUserDaoImpl.loadById(wxuser.getUserParentId());
							WxUserRewardLogDTO w1 = new WxUserRewardLogDTO();
							w1.setRewardUserId(wxuser.getUserParentId());
							w1.setRewardMoney(new BigDecimal(1));
							w1.setUserPhone(d1.getULoginName());
							w1.setActivityCode(ACTIVITY_CODE);
							w1.setDeleteTag("1");
							FpFaUserDTO d1s = fpUserDaoImpl.queryByMobile(wxuser.getUserPhone());
							w1.setRewardSourceUserId(d1s.getId());
							List<WxUserRewardLogDTO> ls1 = wxUserRewardLogDaoImpl.queryList(w1);
							if (ls1.size() == 0) {
								w1.setRewardDesc("英雄帖活动一级奖励，来自用户:"+wxuser.getUserPhone());
								w1.setCreateDate(new Date());
								w1.setCreateBy(d1s.getId()+"");  //上级
								w1.setRewardStatus(2);
								w1.setIsRole(2);
								w1.setCreateDate(new Date());
								wxUserRewardLogDaoImpl.insert(w1);
							}
							WxUserInfoDTO wxuser2 = new WxUserInfoDTO();
							wxuser2.setUserPhone(d1.getULoginName());
							wxuser2.setUserActivityCode(ACTIVITY_CODE);
							WxUserInfoDTO wxusers = wxuserDaoImpl.loadByUserPhone(wxuser2);
							// 上上级
							if (wxusers!=null && wxusers.getUserParentId()!=null && wxusers.getUserParentId()!=0) {
								FpFaUserDTO ds = fpUserDaoImpl.loadById(wxusers.getUserParentId());
								WxUserRewardLogDTO w2 = new WxUserRewardLogDTO();
								w2.setRewardUserId(ds.getId());
								w2.setRewardMoney(new BigDecimal(0.5));
								w2.setUserPhone(ds.getULoginName());
								w2.setActivityCode(ACTIVITY_CODE);
								w2.setDeleteTag("1");
								w2.setRewardSourceUserId(d1s.getId());
								List<WxUserRewardLogDTO> ls2 = wxUserRewardLogDaoImpl.queryList(w2);
								if (ls2.size() == 0) {
									w2.setRewardDesc("英雄帖活动二级奖励，来自用户:"+wxuser.getUserPhone());
									w2.setRewardStatus(2);
									w2.setIsRole(2);
									w2.setCreateBy(d1s.getId()+"");
									w2.setCreateDate(new Date());
									wxUserRewardLogDaoImpl.insert(w2);
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	

}